/*
 * WorldWindUtil.java
 *
 * Created on Aug 17, 2007, 1:42:08 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oslo.ui.util;

import gov.nasa.worldwind.AVKey;
import gov.nasa.worldwind.Model;
import gov.nasa.worldwind.WWIcon;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.IconLayer;
import org.oslo.domain.HotelInfo;
import org.oslo.domain.LocationInfo;
import org.oslo.ui.util.SessionComboBoxModel.SessionLocationPair;

/**
 * Simple helper class that helps working with the worldwind model and canvas
 *
 * @author nl24167
 */
public class WorldWindUtil {

    /**
     * Create a new model and add it to the worldwindcanvas
     */
    public static Model initModel(WorldWindowGLCanvas canvas) {
        Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
        canvas.setModel(m);

        return m;
    }

    /**
     * Create a new iconlayer and add this layer to the canvas
     */
    public static IconLayer createIconLayer(WorldWindowGLCanvas canvas) {
        IconLayer iconLayer = new IconLayer();
        canvas.getModel().getLayers().add(iconLayer);

        return iconLayer;
    }

    public static HotelWWIcon addHotelIcon(String iconImage, LocationInfo location, HotelInfo hotel, IconLayer layer) {
        HotelWWIcon icon = new HotelWWIcon(iconImage, 
                new Position(Angle.fromDegrees(location.getLatitude()), Angle.fromDegrees(location.getLongitude()), 0),
                hotel);

        layer.addIcon(icon);

        return icon;
    }

    public static WWIcon addIcon(String iconImage, LocationInfo location, IconLayer iconLayer) {
        WWIcon icon = new CorrectWWIcon(iconImage, new Position(Angle.fromDegrees(location.getLatitude()), Angle.fromDegrees(location.getLongitude()), 0));

        iconLayer.addIcon(icon);

        return icon;
    }

//
//        canvas.addSelectListener(new SelectListener() {
//            private WWIcon lastToolTipIcon = null;
//
//            public void selected(SelectEvent event) {
//                if (event.getEventAction().equals(SelectEvent.LEFT_CLICK)) {
//                    if (event.hasObjects()) {
//                        if (event.getTopObject() instanceof PropertyWWIcon) {
//                            String fileLocation = ((PropertyWWIcon)event.getTopObject()).getProperty();
//                            String[] locations = fileLocation.split(",");
//                            for (String foundLocation : locations) {
//                                FlickrDialog dialog = new FlickrDialog(DesktopApplication2Frame.this,true,foundLocation);
//                                dialog.setVisible(true);
//                            }
//                        }
//                    }
//                } else if (event.getEventAction().equals(SelectEvent.HOVER)) {
//                    if (lastToolTipIcon != null) {
//                        lastToolTipIcon.setShowToolTip(false);
//                        this.lastToolTipIcon = null;
//                       // DesktopApplication2Frame.this.worldWindowGLCanvas1.repaint();
//                    }
//
//                    if (event.hasObjects()) {
//                        if (event.getTopObject() instanceof WWIcon) {
//                            this.lastToolTipIcon = (WWIcon) event.getTopObject();
//                            lastToolTipIcon.setShowToolTip(true);
//                         //   DesktopApplication2Frame.this.worldWindowGLCanvas1.repaint();
//                        }
//                    }
//                } else if (event.getEventAction().equals(SelectEvent.ROLLOVER))  {
//                    DesktopApplication2Frame.this.highlight(event.getTopObject());
//                }
//            }
//        });
//    }
//    }
}
